﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DateExtract;
using HtmlAgilityPack;
using System.Collections;
using System.IO;


namespace RemoveRelatedNews
{
    public partial class Form1 : Form
    {
        private ArrayList ListKeyword()
        {
            ArrayList col = new ArrayList();
            col.Add(new DictionaryEntry("[Lựa chọn keywords]", ""));
            col.Add(new DictionaryEntry("[>>]", "&gt;&gt;"));
            col.Add(new DictionaryEntry("[>]", "<br>>"));
            return col;
        }
        private ArrayList ListXpath()
        {
            ArrayList col = new ArrayList();
            col.Add(new DictionaryEntry("[Lựa chọn Xapth Of Page]", ""));
            col.Add(new DictionaryEntry("landtoday", "//div[@id ='srAll']")); col.Add(new DictionaryEntry("baocongthuong.com", "//a[@class ='Lead']"));
            col.Add(new DictionaryEntry("bongda.com.vn", "//a[@class ='read_more']")); col.Add(new DictionaryEntry("bee.net", "//table[@class ='rl center']"));
            col.Add(new DictionaryEntry("khoahoc.com.vn", "//div[@id ='rPanel1']/table/tbody/tr/td/table/tbody/tr[6]/*")); col.Add(new DictionaryEntry("vovnews.vn", "//ul[@class ='story-listing']"));
            col.Add(new DictionaryEntry("sggp.org.vn", "//tbody/tr/td/p/strong|//tbody/tr/td/p/font")); col.Add(new DictionaryEntry("docbaogame.com", "//div[@class ='field-items]/div"));
            col.Add(new DictionaryEntry("thethao.zing.vn", "//p[@class ='pBody']/a")); col.Add(new DictionaryEntry("zing.vn", "//p[@class = 'pBody']/a"));
            col.Add(new DictionaryEntry("phapluattp.vn", "//ul[@class ='ul11 mt1']")); col.Add(new DictionaryEntry("giadinh.net.vn", "//div[@class ='full-post']/div/div/a|//div[@class ='full-post']/div/div/div/div/a"));
            col.Add(new DictionaryEntry("tintuconline.com.vn", "//table[@class ='newsRelate']"));
            col.Add(new DictionaryEntry("sgtt.vn", "//div[@class ='boxContentNewsDetail']/div/p[@class ='pBody']/strong|//div[@class ='boxContentNewsDetail']/div/ul|//div[@class ='boxTinbailQuan']"));
            col.Add(new DictionaryEntry("infotv.vn", "//div[@class ='article-content']/p[2]/a[@class ='related-news']"));
            col.Add(new DictionaryEntry("diaoc.tuoitre.vn", "//p[@class ='pBody']/a|//p[@class='pBody']/font/a|//p[@class ='pBody']/strong"));
            col.Add(new DictionaryEntry("phapluatxahoi.vn", "//ul[@class= 'recent']")); col.Add(new DictionaryEntry("vnexpress.net", "//h2/a[@class ='Lead']"));
            col.Add(new DictionaryEntry("sohoa.vnexpress.net", "//a[@class ='Lead']|//h2[@class='Lead']/u/a|//h2[@class='Lead']/font/u/a"));
            col.Add(new DictionaryEntry("congluan.vn", "//a/span"));
            return col;
        }
        public Form1()
        {
            InitializeComponent();
            cbKeywordRemove.DisplayMember = "Key";
            cbKeywordRemove.ValueMember = "Value";
            cbKeywordRemove.DataSource = ListKeyword();
            cbXapth.DisplayMember = "Key";
            cbXapth.ValueMember = "Value";
            cbXapth.DataSource = ListXpath();
            webBrowser1.Navigate("about:blank");
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            var Err = string.Empty;
            try
            {
                var CurrentKeywordSpecial2 = string.Empty;
                var NameUrl = txtLink.Text;
                if (string.IsNullOrEmpty(NameUrl))
                {
                    Err = "Bạn hãy nhập link của bài viết muốn test vào";
                }
                StringBuilder CurrentHtml = new StringBuilder();
                HtmlWeb Hw = new HtmlWeb();
                HtmlAgilityPack.HtmlDocument doc = Hw.Load(NameUrl);
                HtmlNodeCollection CU = doc.DocumentNode.SelectNodes("//*");
                var CurrentStringHtml = CurrentHtml.Append(CU[0].OuterHtml).ToString();
                var CurrentxPath = cbXapth.SelectedValue.ToString();
                var CurrentKeywordSpecial1 = cbKeywordRemove.SelectedValue.ToString();
                if (CurrentKeywordSpecial1 == "&gt;&gt;")
                {
                    CurrentKeywordSpecial2 = ">>";
                }
                if (CurrentKeywordSpecial1 == "<br>>")
                {
                    CurrentKeywordSpecial2 = "&gt;";
                }
                var b = new RemoveRelateNews(CurrentStringHtml, CurrentxPath, CurrentKeywordSpecial1, CurrentKeywordSpecial2);


                 txtHtml.Text = b.DataContent;
                 TextWriter w = new FileInfo("Test.html").CreateText();
                 w.Write(b.DataContent);
                 w.Close();


            }
            catch (System.Exception err)
            {
                MessageBox.Show("Error:", Err);
            }
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            System.Threading.Thread.Sleep(5000);
            webBrowser1.Document.Body.InnerHtml = txtHtml.Text;
             
        }

    }
}
